/*
 * @Author: ziqi jhzq12345678
 * @Date: 2024-08-20 09:55:00
 * @LastEditors: wangdazhuang
 * @LastEditTime: 2025-01-27 20:31:44
 * @FilePath: /pornhub_app/lib/views/search/result/search_result_page.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:pornhub_app/components/app_bar/app_bar_view.dart';
import 'package:pornhub_app/components/tab_bar_indicator/easy_fixed_indicator.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/search/result/search_topic_result.dart';
import 'package:pornhub_app/views/search/result/search_user_result.dart';
import 'package:pornhub_app/views/search/result/search_dynamic_result.dart';
import 'package:pornhub_app/views/search/result/search_short_result.dart';
import 'package:pornhub_app/views/search/search_controller.dart';
import 'package:pornhub_app/views/search/result/search_video_result.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/views/search/search_user_controller.dart';

import 'search_user_short_result.dart';
import 'search_user_video_result.dart';

class SearchUserResultPage extends StatefulWidget {
  const SearchUserResultPage({super.key});

  @override
  State createState() => _SearchUserResultPagePage();
}

class _SearchUserResultPagePage extends State<SearchUserResultPage>
    with SingleTickerProviderStateMixin {
  TextEditingController searchTextController = TextEditingController();

  final searchvc = Get.find<SearchUserPageController>();

  late TabController tabController;
  int user_id = Get.arguments["user_id"] ?? 0;
  String user_name = Get.arguments["word"] ?? "";
  final _tabs = [
    {
      'name': '视频',
      'type': 1,
    },
    {
      'name': '刷片',
      'type': 2,
    },
  ];
  var activeTab = 0;

  void handleSearch([String? v]) {
    if (searchTextController.text.isEmpty && v == null) return;
    String words = v ?? searchTextController.text;
    searchvc.addHistory(words, uid: user_id);
  }

  @override
  void initState() {
    if (user_id == 0) {
      searchTextController.text = Get.arguments['word'];
    }

    tabController = TabController(
      length: _tabs.length,
      vsync: this,
    )..addListener(() {
        if (tabController.index.toDouble() == tabController.animation?.value) {
          activeTab = tabController.index;
        }
      });
    super.initState();
  }

  @override
  void dispose() {
    tabController.dispose();
    super.dispose();
  }

  initListWidget(type) {
    switch (type) {
      case 1:
        return SearchUserVideoResult(searchType: type);
      case 2:
        return SearchUserShortResult(
          searchType: type,
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: _tabs.length,
        child: Scaffold(
          appBar: AppBarView(titleText: user_name),
          body: Column(
            children: [
              Column(
                children: [
                  Flex(
                    direction: Axis.horizontal,
                    children: [
                      Expanded(
                          child: Container(
                              width: 296.w,
                              height: 30.w,
                              clipBehavior: Clip.hardEdge,
                              alignment: Alignment.center,
                              margin: EdgeInsets.only(right: 20.w, left: 20.w),
                              padding: EdgeInsets.symmetric(horizontal: 15.w),
                              decoration: BoxDecoration(
                                border: Border.all(color: Colors.white),
                                borderRadius: BorderRadius.circular(15.w),
                              ),
                              child: Flex(
                                direction: Axis.horizontal,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  Image.asset(
                                    'assets/search/ic_search.png',
                                    width: 16.w,
                                    height: 16.w,
                                  ),
                                  Padding(padding: EdgeInsets.only(left: 8.w)),
                                  Expanded(
                                      child: TextField(
                                    style: TextStyle(
                                        color: Colors.white, fontSize: 14.w),
                                    controller: searchTextController,
                                    onSubmitted: (value) {
                                      searchvc.addHistory(value, uid: user_id);
                                      handleSearch(value);
                                    },
                                    decoration: InputDecoration(
                                        isCollapsed: true,
                                        hintText: "输入搜索内容",
                                        hintStyle: TextStyle(
                                            color: COLOR.hexColor('#898A8E'),
                                            fontSize: 14.w),
                                        enabledBorder:
                                            const UnderlineInputBorder(
                                                borderSide: BorderSide(
                                                    color: Color
                                                        .fromRGBO(0, 0, 0, 0))),
                                        focusedBorder:
                                            const UnderlineInputBorder(
                                                borderSide: BorderSide(
                                                    color: Color.fromRGBO(
                                                        0, 0, 0, 0)))),
                                  ))
                                ],
                              ))),
                      GestureDetector(
                        onTap: handleSearch,
                        child: Text(
                          '搜索',
                          style: TextStyle(
                              color: COLOR.hexColor('#B93FFF'), fontSize: 15.w),
                        ),
                      ).paddingRight(14.w)
                    ],
                  ),
                  TabBar(
                      tabAlignment: TabAlignment.fill,
                      isScrollable: false,
                      labelColor: COLOR.hexColor('#B93FFF'),
                      unselectedLabelColor: COLOR.hexColor('#808080'),
                      indicatorColor: COLOR.hexColor('#B93FFF'),
                      labelStyle: TextStyle(
                          fontSize: 14.w, fontWeight: FontWeight.w600),
                      unselectedLabelStyle: TextStyle(
                          fontSize: 14.w, fontWeight: FontWeight.w500),
                      dividerColor: Colors.transparent,
                      controller: tabController,
                      indicator: EasyFixedIndicator(
                        color: COLOR.hexColor('#B93FFF'),
                        width: 10.w,
                        height: 3.w,
                      ),
                      tabs:
                          _tabs.map((v) => Tab(text: '${v['name']}')).toList()),
                  Container(
                    height: 1.w,
                    color: COLOR.hexColor('#292A31'),
                  )
                ],
              ),
              Expanded(
                  child: TabBarView(
                controller: tabController,
                children: _tabs.map((v) {
                  return Container(
                      margin: EdgeInsets.only(top: 10.w),
                      child: initListWidget(v['type']));
                }).toList(),
              ))
            ],
          ),
        ));
  }
}
